package com.hb.software.controller;

import com.hb.software.domain.BaseEntity;
import com.hb.software.domain.Result;
import com.hb.software.service.BaseService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public abstract class BaseController <T extends BaseEntity<PK>,PK extends Serializable,M extends BaseService<T,PK>>{
    protected M manager;
    final Logger logger = LogManager.getLogger(this.getClass().getName());

    @PostMapping("/findOne")
    public Result findOne(@RequestBody String id){
        List list = new ArrayList();
        list.add(manager.findOne(Long.valueOf(id.substring(0,id.length()-1))));
        return Result.success(list);
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody String id){

        manager.delete(Long.valueOf(id.substring(0,id.length()-1)));
        return Result.success();
    }
    @PostMapping("/update")
    public Result update(@RequestBody T t){
        manager.update(t);
        return Result.success();

    }
    @PostMapping("/save")
    public Result save(@RequestBody T t ){
        manager.save(t);
        return Result.success();
    }
    @PostMapping("/findList")
    public Result findList(@RequestBody String id){
        List list = new ArrayList();
        list = manager.findList(Long.valueOf(id.substring(0,id.length()-1))+19);
        return Result.success(list);
    }
    @GetMapping("/findByPage")
    public Map<String, Object> findByPage(Integer pageNow, Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        pageNow = pageNow == null ? 1 : pageNow; // 不传当前页数默认为1
        pageSize = pageSize == null ? 4 : pageSize; // 不传当前页面显示条数默认显示4条
        List<T> salaries = manager.findByPage(pageNow,pageSize); // 分页查询
        Long totals = manager.findTotals();
        result.put("salaries", salaries);
        result.put("total", totals);
        return result;
    }
}
